#include <iostream>
#include <unistd.h>
#include <vector>
#include <sys/types.h>
using namespace std;

const int num = 10;

void SubProcessRun()
{
    while(true)
    {
        cout << "I am a sub process, pid: "<< getpid() << " ,ppid: "<<getppid()<<endl;
        sleep(5);
    }
}

int main()
{   
    vector<pid_t> childproc;
    for(int i = 0; i < num; i++)
    {
        pid_t id = fork();
        if (id == 0)
        {
            //子进程
            SubProcessRun();
        }
        //走到这里，只能是父进程执行
        childproc.push_back(id);
    }
    
    //父进程遍历所有子进程的pid
    cout << "我的所有子进程:";
    for(auto child: childproc)
    {
        cout<<child << " ";
    }
    cout << endl;
    sleep(10);

    while(true)
    {
        cout << "我是父进程，pid: "<< getpid()<<endl;
        sleep(1);
    }

    return 0;
}
